package com.talkingdata.hadoop.util

import com.talkingdata.hadoop.bean.{DfsClusterState, NameNodeState, RmState, XmlConfig, YarnClusterState}
import io.getquill.{MysqlJdbcContext, SnakeCase}

/**
	* getQuill
	* 	数据库模式被case class代表,case class的类名和字段名最好与数据库中的模式相同,
	* 	如果表名、字段名  与 实体类不一致，需要自定义映射关系
	*/

case class QuillCon() {
	lazy val ctx = new MysqlJdbcContext(SnakeCase, "ctx")
	import ctx._
	def insertXmlConfig(xc: XmlConfig): Unit = {
		ctx.run(
			query[XmlConfig].insert(lift(xc))
				.returningGenerated(_.id)
			)
		}

	def selectAllXmlConfig(): List[XmlConfig] = {
		ctx.run(query[XmlConfig])
	}

	def selectConfigureByServiceId(ServiceId: String): List[XmlConfig] ={
		ctx.run(query[XmlConfig].filter(_.serviceId == lift(ServiceId)))
	}
	def selectServiceId(): List[String]  ={
		ctx.run(query[XmlConfig].map(_.serviceId))
	}

	def insertDfsClusterState(dc: DfsClusterState): Unit = {
		ctx.run(query[DfsClusterState].insert(lift(dc)).returningGenerated(_.id))
		}

	def selectDfsClusterState(service_id:String): DfsClusterState ={
		ctx.run(query[DfsClusterState].filter(_.serviceId == lift(service_id))).maxBy(_.id)
	}

	def insertSingleNameNodeInfo(nn: NameNodeState): Unit = {
		ctx.run(query[NameNodeState].insert(lift(nn)).returningGenerated(_.id))
	}

	def batchInsertNameNode(nn: List[NameNodeState]): Unit = {
		ctx.run(liftQuery(nn).foreach(p =>
			query[NameNodeState].insert(p).returningGenerated(_.id)
		))
	}
    def selectNameNodeinfo(service_id: String): Unit = {
        val s:NameNodeState = ctx.run(query[NameNodeState].filter(_.serviceId == lift(service_id))).maxBy(_.id)
    }

	/**
		* YarnClusterState 操作
		* @param ycs
		*/

	def insertYarnClusterState(ycs: YarnClusterState): Unit = {
		ctx.run(query[YarnClusterState].insert(lift(ycs)).returningGenerated(_.id))
	}

	def batchInsertRM(rm: List[RmState]): Unit = {
		ctx.run(liftQuery(rm).foreach(p =>
			query[RmState].insert(p).returningGenerated(_.id)
		))
	}
}
